package ro.ubbcluj.cs.damate.utils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Lóránd Fazakas (lfazakas)
 *         Date: 2013.06.28.
 *         Time: 11:30
 */
public class PluginEventConstants {

    public static final String ACTION = "action";

    public static final String WORKSPACE = "workspace";
    public static final String PROJECT = "project";
    public static final String RESOURCE = "resource";
    public static final String CLOSED = "closed";
    public static final String BUILT = "built";
    public static final String CREATED = "created";
    public static final String CHANGED = "changed";
    public static final String DELETED = "deleted";

    public static final String WORKBENCH = "workbench";
    public static final String STARTUP = "startup";
    public static final String SHUTDOWN = "shutdown";

    public static final String COMMAND = "command";
    public static final String COMMAND_ID = "commandId";
    public static final String EXECUTED = "executed";
    public static final String FAILED = "failed";
    public static final String NO_HANDLER = "noHandler";

    public static final int CREATED_CODE = 1;
    public static final int DELETED_CODE = 2;
    public static final int CHANGED_CODE = 3;

    public static final int CLOSED_CODE = 4;
    public static final int BUILT_CODE = 5;

    public static final int STARTUP_CODE = 11;
    public static final int SHUTDOWN_CODE = 12;

    public static final int EXECUTED_CODE = 21;
    public static final int FAILED_CODE = 22;
    public static final int NO_HANDLER_CODE = 23;

    public static int getEventCode(String name) {
        return name != null && actions != null ? actions.get(name) : 0;
    }

    public static String getEventName(int code) {
        code = code >= 100 ? code % 100 : code;
        if (actions != null) {
            for(Map.Entry<String, Integer> action : actions.entrySet()) {
                if (action.getValue().equals(code)) {
                    return action.getKey();
                }
            }
        }
        return null;
    }

    private static Map<String, Integer> actions;

    static {
        actions = new HashMap<String, Integer>();
        actions.put(CREATED, CREATED_CODE);
        actions.put(DELETED, DELETED_CODE);
        actions.put(CHANGED, CHANGED_CODE);

        actions.put(CLOSED, CLOSED_CODE);
        actions.put(BUILT, BUILT_CODE);

        actions.put(STARTUP, STARTUP_CODE);
        actions.put(SHUTDOWN, SHUTDOWN_CODE);

        actions.put(EXECUTED, EXECUTED_CODE);
        actions.put(FAILED, FAILED_CODE);
        actions.put(NO_HANDLER, NO_HANDLER_CODE);
    }
}
